package com.ccloud.common.core.utils.zwfw;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author chentailai
 * 短信对接
 * @date 2021/2/25 14:40
 */
public class SmsUtils {

    public String sendSms(String content, String mobile) {
        String simd = "";
        // 1、获取token
        String tokenResponse = HttpRequest.post("http://10.41.215.200:9090/oauth/token?client_id=02b5eecbde0b42b791c31383515a4e38&client_secret=24bbb4b891414b29972b8905c1620194").execute().body();;
        String token = JSONObject.parseObject(tokenResponse).getString("access_token");
        // 2、发送短信
        String sendSmsResponse = doSendSms(mobile, content, token);
        String retCode = JSONObject.parseObject(sendSmsResponse).getString("retCode");
        if ("ok".equals(retCode) || "success".equals(retCode)) {
            System.out.println("发送成功");
            simd = "finish";
        } else {
            System.out.println("发送失败");
        }
        return simd;
    }

    /**
     * 真正的发送短信方法
     */
    private String doSendSms(String mobile, String content, String token) {
        JSONObject smsParam = new JSONObject();
        smsParam.put("action", "sendsms");
        smsParam.put("userName", "whzwfw");
        String time = String.valueOf(System.currentTimeMillis());
        smsParam.put("time", time);
        smsParam.put("sign", DigestUtils.md5Hex("whzwfw" + time + "01hsUb5mtHUZw3Utj6AnJNKFgkuudHa9"));
        smsParam.put("extCode", mobile);
        // 短信模板前必须加上"【武汉政务服务】"
        smsParam.put("content", "【武汉政务服务】" + content);
        smsParam.put("mobile", mobile);
        return HttpRequest.post("http://10.41.215.200:9090/service/api/sms_api/api/init/sendsms?client_id=02b5eecbde0b42b791c31383515a4e38"  + "&access_token=" + token).execute().body();
    }

}
